<template>
  <div class="table">
    <div class="search">
      <span>
        <span style="margin-right: 12px">数据源名称：</span>
        <el-input v-model="tableParam.name" style="width: 240px" clearable />
      </span>
      <el-button icon="el-icon-search" size="medium" class="bottom_all" @click="(tableParam.page = 1), gettableData()">查 询</el-button>
      <el-button size="medium" class="bottom_all_two" style="margin-left: 12px" @click="init()">重 置</el-button>
    </div>
    <div class="search">
      <el-button size="small" class="bottom_all" style="margin-left: 0" @click="addFromDate('添加')">
        <i class="icon iconfont iconicon_button_add" style="font-size: 12px"></i>
        添加
      </el-button>
    </div>
    <div v-loading="tableLoading">
      <el-table :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width: 100%" :data="tableData" tooltip-effect="dark">
        <el-table-column align="center" label="数据源名称" prop="name" min-width="160"></el-table-column>
        <el-table-column align="center" label="链接" prop="dbUrl" min-width="240"></el-table-column>
        <el-table-column align="center" label="类型" prop="dbType" min-width="160"></el-table-column>
        <el-table-column align="center" label="创建时间" prop="createTimeStr" min-width="160"></el-table-column>
        <el-table-column align="center" label="更新时间" prop="updateTimeStr" min-width="160"></el-table-column>
        <el-table-column align="center" label="操作" min-width="180">
          <template slot-scope="scope" fixed="right">
            <el-button class="bottom_one" size="mini" @click=" updateFromDate(scope.row, '修改' )">
              <i class="icon iconfont iconicon_button_modification" style="font-size: 12px"></i>
              修改
            </el-button>
            <el-button size="mini" class="bottom_two" @click="delTable(scope.row.id)">
              <i class="icon iconfont iconicon_button_delete" style="font-size: 12px"></i>
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="card_bottom">
        <div class="card_bottom_left">
          <div class="top_page_totle">总共{{ total }}条</div>
          <el-pagination :current-page.sync="tableParam.page" :page-size="tableParam.pageSize" layout="prev, pager, next, jumper" :total="total" @current-change="gettableData()"></el-pagination>
        </div>
        <div>
          <el-button size="mini" icon="el-icon-refresh-right" class="bottom_all_two" @click="gettableData">刷新</el-button>
        </div>
      </div>
    </div>
    <el-dialog v-dialogDrag :visible.sync="addDialog" width="480px" :close-on-click-modal="false" :before-close="resetFromDate">
      <span slot="title" class="dialog-title">{{ addDialogTitle }}</span>
      <div class="dialoginfo">
        <el-form ref="reffromDate" :rules="rules" :inline="true" :model="fromDate" label-width="110px">
          <el-form-item prop="name">
            <span slot="label" style="color: #ff0000">数据源名称</span>
            <el-input v-model="fromDate.name" placeholder="请输入" clearable>
            </el-input>
          </el-form-item>
          <el-form-item prop="dbType">
            <span slot="label" style="color: #ff0000">类型</span>
            <el-select v-model="fromDate.dbType" placeholder="请选择" @change="change(fromDate.dbType)">
              <el-option value="mysql"></el-option>
              <el-option value="oracle"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="dbUrl">
            <span slot="label" style="color: #ff0000">链接</span>
            <el-input v-model="fromDate.dbUrl" class="textarea" type="textarea" resize="none" rows="7" placeholder="请输入" clearable>
            </el-input>
          </el-form-item>
          <el-form-item prop="dbAccount">
            <span slot="label" style="color: #ff0000">账号</span>
            <el-input v-model="fromDate.dbAccount" placeholder="请输入" clearable>
            </el-input>
          </el-form-item>
          <el-form-item prop="dbPassword">
            <span slot="label" style="color: #ff0000">密码</span>
            <el-input v-model="fromDate.dbPassword" type="password" show-password placeholder="请输入" clearable>
            </el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="resetFromDate">取 消</el-button>
        <el-button class="bottom_all" style="margin-left: 24px" @click="submitFromDate()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { query_page, save_or_update, update_status, get_dbdriver } from './sync.js'
export default {
  data() {
    return {
      tableParam: {
        page: 1,
        pageSize: 12,
        name: '',
      },
      pagesType: 0,
      /*添加表单数据*/
      rules: {
        name: [{ required: true, message: "请输入数据源名称", trigger: "blur" }],
        dbUrl: [{ required: true, message: "请输入链接", trigger: "blur" }],
        dbType: [{ required: true, message: "请选择类型", trigger: "change" }],
        dbAccount: [{ required: true, message: "请输入账号", trigger: "blur" }],
        dbPassword: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      tableData: [],
      tableLoading: false,
      total: 0,
      addDialog: false,
      addDialogTitle: '',
      fromDate: {
        dbUrl: '',
        dbType: ''
      }
    }
  },
  created() {
    this.gettableData()
  },
  methods: {
    async change(dbType) {
      const { data } = await get_dbdriver({ dbType: dbType })
      console.log(data)
      this.fromDate.dbUrl = data
    },
    delTable(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        cancelButtonClass: 'bottom_all_two',
        confirmButtonClass: 'bottom_all',
        type: 'warning'
      }).then(() => {
        update_status({ id: id, status: 0 }).then(res => {
          if (res.code == 1001) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.gettableData();
          }
        })
      }).catch(() => {
      });
    },
    /*关闭添加弹窗并重置数据*/
    resetFromDate() {
      delete this.fromDate.id
      this.addDialog = false
    },
    /*添加*/
    addFromDate(text) {
      this.addDialog = true
      this.$data.fromDate = this.$options.data().fromDate
      this.addDialogTitle = text
      setTimeout(() => {
        this.$refs.reffromDate.clearValidate();
      }, 10)
    },
    submitFromDate() {
      save_or_update(this.fromDate).then(res => {
        console.log(res)
        if (res.code === 1001) {
          this.$message({
            message: '保存成功',
            type: "success",
          });
          this.resetFromDate()
          this.gettableData()
        }
      })
    },
    /*修改数据打开时*/
    updateFromDate(data, text) {
      this.addDialog = true
      setTimeout(() => {
        this.$refs.reffromDate.clearValidate();
      }, 10)
      this.fromDate = Object.assign({}, this.fromDate, data)
      this.addDialogTitle = text
    },
    async gettableData() {
      this.tableLoading = true
      const res = await query_page(this.tableParam)
      this.total = res.data.totalRows;
      this.tableData = res.data.data;
      this.tableLoading = false
    }
  }

}
</script>
<style lang="scss" scoped>
.table {
  padding: 20px;
}
.search {
  padding: 0 0 20px 0;
  justify-content: flex-start;
}
.card_bottom,
.card_bottom_left {
  display: flex;
}
.card_bottom {
  padding: 20px 20px 0px 20px;
  justify-content: space-between;
}
</style>


